#!/bin/bash


dtype=double
dsize=$(( 64*1024*1024 ))
dtimes=10
doff=0
dmode="single"

while getopts "t:s:i:o:m:" opt; do
    case $opt in
        t)
            dtype=$OPTARG
            ;;
        s)
            dsize=$OPTARG
            ;;
        i)
            dtimes=$OPTARG
            ;;
        o)
            doff=$OPTARG
            ;;
        m)
            dmode=$OPTARG
            ;;
        ?)
            echo "$0 -t <type> -s <size> -i <times> -o <offset> -m <single|omp> -h"
            exit
            ;;
    esac
done

logfile=res_${dmode}_${dtype}.log
echo "input para: type=<$dtype>, size=<$dsize>, offset=<$doff>, times=<$dtimes>, mode=<$dmode>" | tee -a $logfile

runone() {
    make clean

    echo ""
#    echo "default defines: DEF_TYPE=double DEF_SIZE=10000000 DEF_TIMES=10 DEF_OFF=0"
    cpunum=$(lscpu | grep "^CPU(s)" | awk '{print $2}')
#    echo "cpunum=$cpunum"

    DEF_TYPE=${dtype}
    # DEF_SIZE should be : (L3Cache'Size * 1024 * 4.1) / sizeof(DEF_TYPE)
    # (64MByte * 1024 * 1024 * 4.1) / 8 = 34393292, its half is 17196646
    # (64MByte * 1024 * 1024 * 3.8) / 8 = 31876710, its half is 15938355
    DEF_SIZE=$(( ${dsize} / 8))
    DEF_TIMES=${dtimes}
    DEF_OFF=${doff}

    if [ x"${dmode}" == x"single" ]; then
        echo "custom defines (single) : DEF_TYPE=${DEF_TYPE} DEF_SIZE=${DEF_SIZE} DEF_TIMES=${DEF_TIMES} DEF_OFF=${DEF_OFF}" | tee -a $logfile
        make stream_c.exe DEF_TYPE=${DEF_TYPE} DEF_SIZE=${DEF_SIZE} DEF_TIMES=${DEF_TIMES} DEF_OFF=${DEF_OFF} | tee -a $logfile

        ./stream_c.exe | tee -a $logfile
    fi

    if [ x"${dmode}" == x"omp" ]; then
        # DEF_SIZE=$(( 8600000 * ${cpunum} ))  #16000000
        echo "custom defines (openmp) : DEF_TYPE=${DEF_TYPE} DEF_SIZE=${DEF_SIZE} DEF_TIMES=${DEF_TIMES} DEF_OFF=${DEF_OFF}" | tee -a $logfile
        make stream_omp.exe DEF_TYPE=${DEF_TYPE} DEF_SIZE=${DEF_SIZE} DEF_TIMES=${DEF_TIMES} DEF_OFF=${DEF_OFF} | tee -a $logfile

        ./stream_omp.exe | tee -a $logfile
    fi
}

runone
